/***********************************************
 This file is part of redia-lib-utils-ios
.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import "LazyLoadImageView.h"
#import <QuartzCore/QuartzCore.h>
#import "defines.h"

@implementation LazyLoadImageView

@synthesize fImageView;

+ (void)createLazyLoaderWithView:(UIImageView*)view url:(NSURL*)url animationTime:(NSTimeInterval)anim_time delegate:(id<UrlCacheEntryDelegate>)d
{
    //allocates an LazyLoadImageView object and releases it, the subsystem retains the object
    //until the fetch operation is complete.
    
    LazyLoadImageView* __unused temp = [[LazyLoadImageView alloc] initWithView:view url:url animationType:FadeIn animationTime:anim_time delegate:d];
}

+ (void)createLazyLoaderWithView:(UIImageView*)view url:(NSURL*)url animationType:(enum LazyLoadImageViewAnimation)animationType animationTime:(NSTimeInterval)anim_time delegate:(id<UrlCacheEntryDelegate>)d
{
    //allocates an LazyLoadImageView object and releases it, the subsystem retains the object 
    //until the fetch operation is complete.
    
    LazyLoadImageView* __unused temp = [[LazyLoadImageView alloc] initWithView:view url:url animationType:animationType animationTime:anim_time delegate:d];
}

- (id)initWithView:(UIImageView*)view url:(NSURL*)url animationTime:(NSTimeInterval)anim_time delegate:(id<UrlCacheEntryDelegate>)d
{
    return [self initWithView:view url:url animationType:FadeIn animationTime:anim_time delegate:d];
}

- (id)initWithView:(UIImageView*)view url:(NSURL*)url animationType:(enum LazyLoadImageViewAnimation)animationType animationTime:(NSTimeInterval)anim_time delegate:(id<UrlCacheEntryDelegate>)d
{
	self = [super init];
	if (self != nil) {
		self.fImageView = view;
        self.animationType = animationType;
		lazyLoader = [[LazyLoadImage alloc] init];
		animationTime = anim_time;
		delegate = d;
		//fetch image immediately:
		[lazyLoader setUrl:url];
		[lazyLoader loadImageWithDelegate:self];
		
		 //make sure we are surviving until imageLoaded or loadImageError
	}
	return self;
}

-(void)cacheEntry:(UrlCacheEntry *)cacheEntry didLoadObjectFromUrl:(NSURL *)url ofType:(UrlCacheEntryType)type
{
    if (type == UrlCacheEntryTypeImage) {
        UrlCacheImageEntry* imageEntry = (UrlCacheImageEntry*) cacheEntry;
        UIImage* theImage = imageEntry.resultImage;
        if (animationTime>0.0) {
            switch (self.animationType) {
                case FadeIn:
                {
                    fImageView.layer.opacity = 0.0;
                    [fImageView setImage:theImage];
                    [UIView animateWithDuration:animationTime animations:^{ fImageView.layer.opacity = 1.0;	} ];
                }
                    break;
                case FlipLeft:
                case FlipRight:
                case FlipTop:
                case FlipBottom:
                case CurlDown:
                case CurlUp:
                {
                    
                    UIViewAnimationOptions options;
                    switch (self.animationType) {
                        case FlipLeft:
                            options = UIViewAnimationOptionTransitionFlipFromLeft;
                            break;
                        case FlipRight:
                            options = UIViewAnimationOptionTransitionFlipFromRight;
                            break;
                        case FlipBottom:
                            options = UIViewAnimationOptionTransitionFlipFromBottom;
                            break;
                        case FlipTop:
                            options = UIViewAnimationOptionTransitionFlipFromTop;
                            break;
                        case CurlDown:
                            options = UIViewAnimationOptionTransitionCurlDown;
                            break;
                        case CurlUp:
                            options = UIViewAnimationOptionTransitionCurlUp;
                            break;
                        default:
                            options = UIViewAnimationOptionTransitionFlipFromLeft;
                            break;
                    }
                    [UIView transitionWithView:fImageView
                                      duration:animationTime
                                       options:options
                                    animations:^{
                                        fImageView.image = theImage;
                                    }
                                    completion:nil];
                }
                    break;
                default:
                    NSLog(@"LazyLoadImageView: Unsupported animation type.");
                    [fImageView setImage:theImage];
                    break;
            }
        } else {
            [fImageView setImage:theImage];
        }
    }
	[delegate cacheEntry:cacheEntry didLoadObjectFromUrl:url ofType:type];
	delegate=nil;
	
}

- (void)unloadImage
{
    [lazyLoader unloadImage];
    delegate = nil;
    
}

-(void)cacheEntry:(UrlCacheEntry *)cacheEntry didFailOnUrl:(NSURL *)url error:(NSError *)error
{
	[delegate cacheEntry:cacheEntry didFailOnUrl:url error:error];
	delegate = nil;
	//TODO: maybe insert error image - customizable via a property

}

/*
- (void) dealloc
{
	DLog(@"dealloced");
}
*/


@end
